package com.beans;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;

public class LoginForm extends ActionForm{
	String username;
	String password;
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}

	@Override
	public ActionErrors validate(ActionMapping mapping,
			HttpServletRequest request) {
		// TODO Auto-generated method stub
		ActionErrors errors = new ActionErrors();
		
		ActionError error;
		
		System.out.println("Validate login  method is called");

		if (username == null || username.length() == 0) {
			error = new ActionError("login.userNameRequired");
			errors.add("username", error);
		}
		/*
		String expression = "^[\\w\\.-]+@([\\w\\-]+\\.)+[A-Z]{2,4}$";
	    CharSequence inputStr = username;
	    Pattern pattern = Pattern.compile(expression, Pattern.CASE_INSENSITIVE);
	    Matcher matcher = pattern.matcher(inputStr);
	    if (!matcher.matches()) {
	    	error = new ActionError("login.validEmailIdRequired");
			errors.add("invalidemail", error); 
	    }*/
		
	    if (password == null || password.length() == 0) {
			error = new ActionError("login.passwordRequired");
			errors.add("password", error);
		}

		return errors;
	}


}
